/*
 * @Author: 杨上漂
 * @Date: 2024-10-14 15:05:43
 * @LastEditors: 杨上漂
 * @LastEditTime: 2024-10-16 10:41:49
 * @FilePath: \gwlc\assets\scripts\SelectSkillItem.ts
 * @Description: 
 * 
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
 */
import { _decorator, Color, Component, find, instantiate, JsonAsset, Label, Node, Prefab, resources, Sprite, SpriteFrame, tween, v4 } from 'cc';
import { Player } from './Player';
import { gameMgr } from './gameMgr';
import { playerSkillTypeEnum } from '../Enum';
import { EnemyMgr } from './EnemyMgr';
import { UserInfo } from '../startScene/UserInfo';
import { StartSceneGameMgr } from '../startScene/StartSceneGameMgr';
const { ccclass, property } = _decorator;

@ccclass('selectSkillItem')
export class selectSkillItem extends Component {

    @property(Label)
    nameLabel: Label;
    @property(Label)
    desLabel: Label;
    @property(Label)
    levelLabel: Label;
    @property(Sprite)
    icon: Sprite;

    skillCellContent: Node;
    @property(Prefab)
    skillCellItemPre: Prefab

    itemSkilltype;
    itemLevel;
    detailSkillId;
    detailSkillLevel
    thisCongfi;

    protected onLoad(): void {
        this.skillCellContent = find("Canvas/UI/skillCellPanel/content");
    }

    init(randCongfi) {
        this.thisCongfi = randCongfi;
        this.itemSkilltype = randCongfi.playerSkillType;
        this.detailSkillId = randCongfi.skillId;

        //UI
        this.nameLabel.string = randCongfi.name;

        let currSkillLevel;
        if (Player.ins.playerSkillInfoDic.containsKey(this.itemSkilltype)) {
            currSkillLevel = Player.ins.playerSkillInfoDic.get(this.itemSkilltype) + 1;
        }
        else {
            currSkillLevel = 1
        }
        this.itemLevel = currSkillLevel;

        //每个小技能的等级
        let currDetailSkillLevel;
        if (Player.ins.playerSkillInfoDetailDic.containsKey(this.detailSkillId)) {
            currDetailSkillLevel = Player.ins.playerSkillInfoDetailDic.get(this.detailSkillId) + 1;
        }
        else {
            currDetailSkillLevel = 1
        }
        this.detailSkillLevel = currDetailSkillLevel


        this.levelLabel.string = `等级：${currSkillLevel}`


        this.node.on(Node.EventType.TOUCH_START, this.onclickThisCom, this);

        //图标
        resources.load(`${randCongfi.iconPath}/spriteFrame`, SpriteFrame, (err, res) => {
            this.icon.spriteFrame = res;
        })

        //技能描述，除弓箭外，第一次升级技能是解锁技能
        if (this.itemSkilltype != playerSkillTypeEnum.normalArrow) {
            if (Player.ins.playerSkillInfoDic.containsKey(this.itemSkilltype) == false) {
                this.desLabel.string = `解锁${randCongfi.name}`
            }
            else {
                this.desLabel.string = randCongfi.desc;
            }
        }
        else {
            this.desLabel.string = randCongfi.desc;
        }

    }

    /**
     * @description: 升级，解锁技能数值
     * @return {*}
     */
    upSkillLevelValue() {
        console.log(this.thisCongfi, "thisCongfi");
        let attack = 0;
        let slotMun = 0;
        let maxAttackEnenmyMun = 0;
        let isHasEffect;
        let frequency = 0;
        let force=0;
        let sustainTime=0;

        if (this.thisCongfi.effect.attack) {
            attack = this.thisCongfi.effect.attack
        }

        if (this.thisCongfi.effect.slotMun) {
            slotMun = this.thisCongfi.effect.slotMun
        }

        if (this.thisCongfi.effect.maxAttackEnenmyMun) {
            maxAttackEnenmyMun = this.thisCongfi.effect.maxAttackEnenmyMun
        }

        if (this.thisCongfi.effect.isHasEffect) {
            isHasEffect = this.thisCongfi.effect.isHasEffect
        }

        if (this.thisCongfi.effect.frequency) {
            frequency = this.thisCongfi.effect.frequency
        }

        if (this.thisCongfi.effect.force) {
            force = this.thisCongfi.effect.force
        }

        if (this.thisCongfi.effect.sustainTime) {
            sustainTime = this.thisCongfi.effect.sustainTime
        }

        switch (this.itemSkilltype) {
            case playerSkillTypeEnum.normalArrow:
                Player.ins.arrowAttackMun = Player.ins.arrowAttackMun * (1 + attack);
                Player.ins.arrowSlotMun += slotMun;
                Player.ins.arrowMaxAttackEnenmyMun += maxAttackEnenmyMun;
                Player.ins.arrowFrequency = Player.ins.arrowFrequency * (1 - frequency);
                break;
            case playerSkillTypeEnum.fireBall:
                Player.ins.fireBallAttackMun = Player.ins.fireBallAttackMun * (1 + attack);
                Player.ins.fireBallMaxAttackEnenmyMun += maxAttackEnenmyMun;
                Player.ins.fireBallFrequency = Player.ins.fireBallFrequency * (1 - frequency);
                Player.ins.FiringSustainTime=Player.ins.FiringSustainTime* (1 + sustainTime);

                if (this.thisCongfi.effect.isHasEffect && this.thisCongfi.effect.isHasEffect == true) {
                    Player.ins.fireBallIsCanFiring = true;
                }
                break;
            case playerSkillTypeEnum.spike:
                Player.ins.spikeAttackMun = Player.ins.spikeAttackMun * (1 + attack);
                Player.ins.spikeMaxAttackEnenmyMun += maxAttackEnenmyMun;
                Player.ins.spikeFrequency = Player.ins.spikeFrequency * (1 - frequency);

                if (this.thisCongfi.effect.isHasEffect && this.thisCongfi.effect.isHasEffect == true) {
                    Player.ins.fireBallIsCanFiring = true;
                }
                break;
            case playerSkillTypeEnum.starLightBullet:
                Player.ins.starLightBulletAttackMun = Player.ins.starLightBulletAttackMun * (1 + attack);
                Player.ins.starLightMaxAttackEnenmyMun += maxAttackEnenmyMun;
                Player.ins.starLightBulletFrequency = Player.ins.starLightBulletFrequency * (1 - frequency);

                if (this.thisCongfi.effect.isHasEffect && this.thisCongfi.effect.isHasEffect == true) {
                    Player.ins.starLightIsCanThrough = true;
                }
                break;
            case playerSkillTypeEnum.spinArrow:
                Player.ins.spinArrowAttackMun = Player.ins.spinArrowAttackMun * (1 + attack);
                Player.ins.spinArrowMaxAttackEnenmyMun += maxAttackEnenmyMun;
                Player.ins.spinArrowFrequency = Player.ins.spinArrowFrequency * (1 - frequency);
                Player.ins.spinArrowForce = Player.ins.spinArrowForce * (1 + force);
                break;
            case playerSkillTypeEnum.moonSlash:
                Player.ins.MoonAttackMun = Player.ins.MoonAttackMun * (1 + attack);
                Player.ins.moonMaxAttackEnenmyMun += maxAttackEnenmyMun;
                Player.ins.MoonFrequency = Player.ins.MoonFrequency * (1 - frequency);
                Player.ins.moonFrozenSustainTime=Player.ins.moonFrozenSustainTime* (1 + sustainTime);


                if (this.thisCongfi.effect.isHasEffect && this.thisCongfi.effect.isHasEffect == true) {
                    Player.ins.moonIsCanFrozen = true;
                }
                break;

            case playerSkillTypeEnum.light:
                Player.ins.lightAttackMun = Player.ins.lightAttackMun * (1 + attack);
                Player.ins.lightMaxAttackEnenmyMun += maxAttackEnenmyMun;
                Player.ins.lightFrequency = Player.ins.lightFrequency * (1 - frequency);
                Player.ins.lightScorchSustainTime=Player.ins.lightScorchSustainTime* (1 + sustainTime);

                if (this.thisCongfi.effect.isHasEffect && this.thisCongfi.effect.isHasEffect == true) {
                    Player.ins.lightIsCanscorch = true;
                }
                break;
        }

    }

    //点击该组件
    onclickThisCom() {
        gameMgr.ins.isGamePause = false;
        Player.ins.selectSkillPanel.active = false;
        Player.ins.Lv += 1;
        if(Player.ins.Lv==19){
            EnemyMgr.ins.openZombieTide();
        }
        //加入技能格子
        // if (this.itemSkilltype!=playerSkillTypeEnum.normalArrow) {//普通攻击不加入技能格子
            if (Player.ins.playerSkillInfoDic.containsKey(this.itemSkilltype)) {
                Player.ins.skillItemNodeDic.get(this.itemSkilltype).getChildByName("Label").getComponent(Label).string = this.itemLevel + ""
            }
            else {
                let pre = instantiate(this.skillCellItemPre);
                pre.setParent(this.skillCellContent);
                pre.setPosition(0, 0, 0);
                resources.load(`${gameMgr.ins.skillCongfiDic.get(this.itemSkilltype).iconPath}/spriteFrame`, SpriteFrame, (err, res) => {
                    pre.getChildByName("sp").getComponent(Sprite).spriteFrame = res
                })
                pre.getChildByName("Label").getComponent(Label).string = "1"
                Player.ins.skillItemNodeDic.set(this.itemSkilltype, pre);
    
            }
        // }


        if (this.itemSkilltype != playerSkillTypeEnum.normalArrow) {
            if (Player.ins.playerSkillInfoDic.containsKey(this.itemSkilltype)) {
                this.upSkillLevelValue();
            }
        }
        else {
            this.upSkillLevelValue();
        }

        switch (this.itemSkilltype) {
            case playerSkillTypeEnum.fireBall:
                UserInfo.accumulateEvolvePlayerSkillFireBall += 1;
                break;
            case playerSkillTypeEnum.spike:
                UserInfo.accumulateEvolvePlayerSkillSpike += 1;
                break;
            case playerSkillTypeEnum.starLightBullet:
                UserInfo.accumulateEvolvePlayerSkillStarLightBullet += 1;
                break;
        }

        //加入字典
        Player.ins.playerSkillInfoDic.set(this.itemSkilltype, this.itemLevel);
        Player.ins.playerSkillInfoDetailDic.set(this.detailSkillId, this.detailSkillLevel);
        StartSceneGameMgr.ins.saveGameDate();

    }
}


